package org.darkforest.admin.controller;

import org.darkforest.common.services.MansongService;
import org.darkforest.model.PMansong;
import org.darkforest.vo.AjaxReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Administrator on 2016/1/26.
 */
@Controller
@RequestMapping(value = "operation")
public class mansongController {

    @Autowired
    private MansongService mansongService;



    @RequestMapping(value = "mansongList")
    public ModelAndView mansongList(@RequestParam(required = false, defaultValue = "1") int start,
                                    String mansongName,
                                    String storeName,
                                    @RequestParam(required = false, defaultValue = "") String state){
        ModelAndView res = new ModelAndView("operation/mansong/mansong_list");
        PMansong pMansong = new PMansong();
        pMansong.setStart(start);
        pMansong.setMansongName("".equals(mansongName) ? null : mansongName);
        pMansong.setStoreName("".equals(storeName) ? null : storeName);
        pMansong.setState("".equals(state) ? null : new Byte(state));
        pMansong.setResultList(mansongService.findListByCondition(pMansong));
        res.addObject("page",pMansong);
        return  res;
    }

    /**
     * 满送活动详情
     * @param mansongId
     * @return
     */
    @RequestMapping(value = "mansongList/mansongDetail")
    public ModelAndView mansongDetail(Integer mansongId){
        ModelAndView res = new ModelAndView("operation/mansong/mansong_detail");
        PMansong pMansong = mansongService.getPMansongWithRules(mansongId);
        res.addObject("mansong",pMansong);
        return res;
    }


    /**
     * 删除满即送活动 根据ID
     * @param mansongId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "delMansong")
    public @ResponseBody AjaxReturnData delMansong(Integer mansongId ) throws Exception {
        String url = "operation/mansongList";
        AjaxReturnData ajaxReturnData = mansongService.deleMansongActive(mansongId, url);
        return ajaxReturnData;
    }


    /**
     * 修改满即送活动状态
     * @param mansongId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "updateMansongState")
    public @ResponseBody AjaxReturnData updateMansong(Integer mansongId,byte state ) throws Exception {
        String url = "operation/mansongList";
        AjaxReturnData ajaxReturnData = mansongService.updateMansong(mansongId, state,url);
        return ajaxReturnData;
    }

}
